class HttpRequest {
  /** 默认配置项 */
  private static defaultFetchOptions = {
    baseURL: "http://localhost:3000",
    timeout: 10000,
  };
  /** 克隆的fetch对象 */
  private static fetchInstance = $fetch.create(HttpRequest.defaultFetchOptions);

  constructor() {}

  request(url: string, config: object = {}) {
    return new Promise((resolve, reject) => {
      HttpRequest.fetchInstance(url, {
        ...config,
        onRequest({ request, options }) {
          console.log("request ====>", request);
          console.log("options ====>", options);
        },
        onResponse({ request, response, options }) {
          console.log("response ====>", response);
          console.log("res-options ====>", options);
        },
      })
        .then(({ data }: any) => {
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  get(url: string, params?: any, config: object = {}) {
    return this.request(url, {
      method: "GET",
      params,
      ...config,
    });
  }

  post(url: string, data?: any, config: object = {}) {
    return this.request(url, {
      method: "POST",
      body: data,
      ...config,
    });
  }

  delete(url: string, params?: any, config: object = {}) {
    return this.request(url, {
      method: "DELETE",
      params,
      ...config,
    });
  }
}

const http = new HttpRequest();

export default http;
